<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/auth.php';
require_once '../includes/attendance_manager.php';
require_once '../includes/user.php';

// 检查权限
$auth = getAuth();
$auth->requirePermission('super_admin');

// 获取当前用户信息
$currentUser = $auth->getCurrentUser();

// 初始化数据库和考勤管理器
$database = getDatabaseConnection();
$attendanceManager = new AttendanceManager($database);
$userManager = new UserManager($database);

// 处理模板下载
if (isset($_GET['download'])) {
    $templateType = $_GET['download'];
    
    if ($templateType === 'csv') {
        // 设置CSV文件头
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=考勤记录导入模板.csv');
        
        // 创建输出流
        $output = fopen('php://output', 'w');
        
        // 添加BOM头，确保Excel正确显示中文
        fwrite($output, "\xEF\xBB\xBF");
        
        // 写入CSV标题行
        fputcsv($output, [
            '用户名',
            '日期(YYYY-MM-DD)',
            '状态',
            '上午上班时间(HH:MM)',
            '上午下班时间(HH:MM)',
            '下午上班时间(HH:MM)',
            '下午下班时间(HH:MM)',
            '全天工时(小时)',
            '备注'
        ]);
        
        // 写入示例数据
        fputcsv($output, [
            'admin',
            date('Y-m-d'),
            'normal',
            '08:30',
            '12:00',
            '14:00',
            '18:30',
            '',
            '正常上班'
        ]);
        
        fputcsv($output, [
            'user1',
            date('Y-m-d'),
            'business_trip',
            '',
            '',
            '',
            '',
            '8.0',
            '出差记录'
        ]);
        
        fputcsv($output, [
            'user2',
            date('Y-m-d'),
            'leave',
            '',
            '',
            '',
            '',
            '',
            '请假一天'
        ]);
        
        fclose($output);
        exit;
    }
}

// 获取用户列表用于显示
$users = $attendanceManager->getUsersForSelect();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>下载模板 - 考勤管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/style.css" rel="stylesheet">
    <style>
        .template-card {
            border: 2px dashed #dee2e6;
            border-radius: 0.5rem;
            padding: 2rem;
            text-align: center;
            transition: all 0.3s ease;
        }
        .template-card:hover {
            border-color: #007bff;
            background-color: #f8f9fa;
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <?php include_once '../includes/sidebar.php'; ?>

    <!-- 主内容区 -->
    <div class="main-content">
        <!-- 顶部导航 -->
        <nav class="navbar navbar-expand navbar-custom mb-4">
            <div class="container-fluid">
                <div class="navbar-nav me-auto">
                    <span class="navbar-text">
                        <i class="fas fa-download me-2"></i>下载导入模板
                    </span>
                </div>
                <div class="navbar-nav">
                    <span class="navbar-text me-3">
                        <i class="fas fa-user me-2"></i>
                        <?php echo htmlspecialchars($currentUser['full_name']); ?>
                    </span>
                    <a href="../user/profile.php" class="nav-link">
                        <i class="fas fa-cog"></i> 个人设置
                    </a>
                </div>
            </div>
        </nav>

        <div class="row">
            <div class="col-md-8 mx-auto">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">
                            <i class="fas fa-file-download me-2"></i>考勤记录导入模板
                        </h5>
                    </div>
                    <div class="card-body">
                        <!-- 模板下载区域 -->
                        <div class="template-card mb-4">
                            <i class="fas fa-file-csv fa-4x text-primary mb-3"></i>
                            <h4>CSV导入模板</h4>
                            <p class="text-muted mb-3">
                                下载CSV格式的考勤记录导入模板，支持Excel打开和编辑
                            </p>
                            <a href="?download=csv" class="btn btn-primary btn-lg">
                                <i class="fas fa-download me-2"></i>下载CSV模板
                            </a>
                        </div>

                        <!-- 使用说明 -->
                        <div class="alert alert-info">
                            <h6><i class="fas fa-info-circle me-2"></i>使用说明：</h6>
                            <ol class="mb-0">
                                <li>点击上方按钮下载CSV模板</li>
                                <li>使用Excel或其他表格软件打开模板</li>
                                <li>按照模板格式填写考勤记录数据</li>
                                <li>保存文件后，在批量操作页面进行导入</li>
                            </ol>
                        </div>

                        <!-- 数据格式说明 -->
                        <div class="card">
                            <div class="card-header">
                                <h6 class="card-title mb-0">
                                    <i class="fas fa-table me-2"></i>数据格式说明
                                </h6>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead class="table-light">
                                            <tr>
                                                <th>字段名</th>
                                                <th>说明</th>
                                                <th>示例</th>
                                                <th>必填</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>用户名</td>
                                                <td>系统中已存在的用户名</td>
                                                <td>admin, user1, user2</td>
                                                <td>是</td>
                                            </tr>
                                            <tr>
                                                <td>日期</td>
                                                <td>考勤日期，格式：YYYY-MM-DD</td>
                                                <td>2024-01-15</td>
                                                <td>是</td>
                                            </tr>
                                            <tr>
                                                <td>状态</td>
                                                <td>考勤状态，可选值：normal, late, early_leave, absent, leave, business_trip, overtime</td>
                                                <td>normal</td>
                                                <td>是</td>
                                            </tr>
                                            <tr>
                                                <td>上午上班时间</td>
                                                <td>上午上班打卡时间，格式：HH:MM</td>
                                                <td>08:30</td>
                                                <td>否</td>
                                            </tr>
                                            <tr>
                                                <td>上午下班时间</td>
                                                <td>上午下班打卡时间，格式：HH:MM</td>
                                                <td>12:00</td>
                                                <td>否</td>
                                            </tr>
                                            <tr>
                                                <td>下午上班时间</td>
                                                <td>下午上班打卡时间，格式：HH:MM</td>
                                                <td>14:00</td>
                                                <td>否</td>
                                            </tr>
                                            <tr>
                                                <td>下午下班时间</td>
                                                <td>下午下班打卡时间，格式：HH:MM</td>
                                                <td>18:30</td>
                                                <td>否</td>
                                            </tr>
                                            <tr>
                                                <td>全天工时</td>
                                                <td>出差状态时填写全天工时（小时）</td>
                                                <td>8.0</td>
                                                <td>否</td>
                                            </tr>
                                            <tr>
                                                <td>备注</td>
                                                <td>考勤记录备注信息</td>
                                                <td>正常上班</td>
                                                <td>否</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                        <!-- 状态说明 -->
                        <div class="card mt-4">
                            <div class="card-header">
                                <h6 class="card-title mb-0">
                                    <i class="fas fa-info-circle me-2"></i>状态说明
                                </h6>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <ul class="list-unstyled">
                                            <li><span class="badge bg-success me-2">normal</span>正常</li>
                                            <li><span class="badge bg-warning me-2">late</span>迟到</li>
                                            <li><span class="badge bg-danger me-2">early_leave</span>早退</li>
                                            <li><span class="badge bg-secondary me-2">absent</span>缺勤</li>
                                        </ul>
                                    </div>
                                    <div class="col-md-6">
                                        <ul class="list-unstyled">
                                            <li><span class="badge bg-info me-2">leave</span>请假</li>
                                            <li><span class="badge bg-primary me-2">business_trip</span>出差</li>
                                            <li><span class="badge bg-dark me-2">overtime</span>加班</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 操作按钮 -->
                        <div class="text-center mt-4">
                            <a href="attendance_batch_simple.php" class="btn btn-outline-primary me-2">
                                <i class="fas fa-arrow-left me-2"></i>返回批量操作
                            </a>
                            <a href="attendance_enhanced.php" class="btn btn-outline-secondary">
                                <i class="fas fa-clipboard-list me-2"></i>查看考勤记录
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
